/**
 * 
 * @Author: biubiu0002
 * @Date: 2021-04-26 11:44
 **/




#include <StudentScoreMapper.h>
#include <iostream>
#include <CourseSelectionMapper.h>
#include <CourseSelection.h>
#include <CourseSelectionService.h>
#include <StudentMapper.h>
#include <CourseMapper.h>
#include "iomanip"
#include "StudentScoreService.h"

using namespace std;

int StudentScoreService::showStudentScoreByCourse(string courseId){

    StudentScoreMapper studentScoreMapper;
    vector<StudentScore> resultList = studentScoreMapper.selectByCourseId(courseId);
    showStudentScoreList(resultList);
    return 0;


}

int StudentScoreService::showStudentScoreByClass(string classId){
    StudentScoreMapper studentScoreMapper;
    list<StudentScore> studentScoreList = studentScoreMapper.getStudentScoreList();
    vector<StudentScore> resultList;
    StudentMapper studentMapper;
    vector<Student> studentList =  studentMapper.selectStudentByClass(classId);
    for(Student student:studentList){
        string studentId = student.getId();
        for(StudentScore studentScore:studentScoreList){
            if(studentScore.getStudentId()==studentId){
                resultList.push_back(studentScore);
            }
        }
    }
    showStudentScoreList(resultList);
    return 0;

}

int StudentScoreService::showAllStudentScoreList() {
    StudentScoreMapper studentScoreMapper;
    list<StudentScore> studentScoreList = studentScoreMapper.getStudentScoreList();
    return this->showStudentScoreList(studentScoreList);
}

int StudentScoreService::batchInsertStudentScore() {
    CourseSelectionService courseSelectionService;
    StudentMapper studentMapper;
    CourseMapper courseMapper;
    CourseSelectionMapper courseSelectionMapper;
    StudentScoreMapper studentScoreMapper;
    list<StudentScore> studentScoreList = studentScoreMapper.getStudentScoreList();
    list<StudentScore> tempStudentScoreList;

    int onceCommitChoice = -1;
    while(true){
        string studentId,courseId,field;
        float score;//接收成绩输入
        cout<<"请输入学生学号(12位统一编号):";cin>>studentId;
        if(!studentMapper.checkId(studentId)){
            cout<<"没有找到该学号，该学生可能没录入到系统中";
            if(isContinue(studentScoreList,tempStudentScoreList)){
                continue;
            }
            return 0;

        }
        cout<<"请输入课程编号(6位统一编号):";cin>>courseId;
        if(!courseMapper.checkId(courseId)){
            cout<<"没有找到该课程编号，该课程可能没录入到系统中";
            if(isContinue(studentScoreList,tempStudentScoreList)){
                continue;
            }
            return 0;
        }
        vector<CourseSelection> courseSelectionList = courseSelectionMapper.selectByStudentIdAndCourseId(studentId,courseId);
        int courseSelectionListsize = courseSelectionList.size();
        if(courseSelectionListsize<=0){
            cout<<"该学生没有选择该门课程";
            if(isContinue(studentScoreList,tempStudentScoreList)){
                continue;
            }
            return 0;
        }
        StudentScore* studentScore = NULL;

        if(courseSelectionListsize>1){
            cout<<"该学生存在重修情况，请选择要录入哪一条课程信息.(输入对应的行号(从1开始)如1 2 ...)"<<endl;
            courseSelectionService.showCourseSelectionList(courseSelectionList);
            CourseSelection courseSelection;
            while(true){
                int lineChoice = 0;
                cin>>lineChoice;
                if(lineChoice<1||lineChoice>courseSelectionListsize){
                    cout<<"您的输入有误，请重试."<<endl;
                    continue;
                }
                courseSelection = courseSelectionList.at(lineChoice-1);
                studentScore = new StudentScore();
                studentScore->setStudentId(studentId);
                studentScore->setCourseId(courseId);
                studentScore->setSchoolYear(courseSelection.getSchoolYear());
                studentScore->setSemester(courseSelection.getSemester());
                break;
            }

        }
        else{
            CourseSelection courseSelection = courseSelectionList.front();
            studentScore = new StudentScore();
            studentScore->setStudentId(studentId);
            studentScore->setCourseId(courseId);
            studentScore->setSchoolYear(courseSelection.getSchoolYear());
            studentScore->setSemester(courseSelection.getSemester());
        }
        cout<<"请输入学生的平时成绩(保留两位小数)"<<endl;cin>>score;
        studentScore->setRegularScore(score);
        cout<<"请输入学生的考试成绩(保留两位小数)"<<endl;cin>>score;
        studentScore->setExamScore(score);
        cout<<"请输入学生的综合成绩(保留两位小数)"<<endl;cin>>score;
        studentScore->setFinalScore(score);

        cout<<endl<<"您输入的课程成绩信息是："<<endl;
        cout<<"学生学号\t课程编号\t学年\t学期\t平时分\t考试成绩\t综合成绩"<<endl;
        cout<<studentScore->getStudentId()<<" "<<studentScore->getCourseId()<<" ";
        cout<<studentScore->getSchoolYear()<<" "<<studentScore->getSemester()<<" ";
        cout<<fixed<<setprecision(2)<<studentScore->getRegularScore()<<" ";
        cout<<fixed<<setprecision(2)<<studentScore->getExamScore()<<" ";
        cout<<fixed<<setprecision(2)<<studentScore->getFinalScore()<<" ";
        cout<<endl<<"确认该条记录正确吗?(1.正确，加入到暂存区  2.错误，重新输入  0.放弃输入，退回上一页面)";
        cin>>onceCommitChoice;
        if(onceCommitChoice==0){
            cout<<"退出成功";
            system("pause");
            system("cls");
            return 0;
        }
        else if(onceCommitChoice==2){
            continue;
        }
        tempStudentScoreList.push_back(*studentScore);
        cout<<"当前已暂存的信息："<<endl;
        cout<<"学生学号\t课程编号\t学年\t学期\t平时分\t考试成绩\t综合成绩"<<endl;
        this->showStudentScoreList(tempStudentScoreList);

        if(isContinue(studentScoreList,tempStudentScoreList)){
            continue;
        }
        return 0;





    }


}


int StudentScoreService::isContinue(list<StudentScore>& studentScoreList,list<StudentScore>& tempStudentScoreList){
    int continueChoice,saveResultChoice;
    cout<<endl<<"是否继续录入？(1.继续录入 0.结束录入)";
    cin>>continueChoice;
    if(continueChoice==1){
        return 1;
    }
    else if(continueChoice==0){
        cout<<"是否保存结果信息？(1.保存 0.放弃，本次录入将不被保存)"<<endl;
        cin>>saveResultChoice;
        if(saveResultChoice){
            for(StudentScore studentScore:tempStudentScoreList){
                studentScoreList.push_back(studentScore);
            }
            StudentScoreMapper studentScoreMapper;
            studentScoreMapper.setStudentScoreList(studentScoreList);
            studentScoreMapper.batchInsertStudentScore(studentScoreList);
            cout<<"保存成功！"<<endl;
        }
        else{
            cout<<"退出成功。"<<endl;
        }
        system("pause");
        system("cls");
        return 0;
    }

    cout<<"程序出错啦，回到上一页面"<<endl;
    system("pause");
    system("cls");
    return 0;
}

int StudentScoreService::showStudentScoreList(list<StudentScore> studentScoreList) {
    for(StudentScore studentScore:studentScoreList){
        cout<<studentScore.getStudentId()<<" ";
        cout<<studentScore.getCourseId()<<" ";
        cout<<studentScore.getSchoolYear()<<" ";
        cout<<studentScore.getSemester()<<" ";
        cout<<fixed<<setprecision(2)<<studentScore.getRegularScore()<<" ";
        cout<<fixed<<setprecision(2)<<studentScore.getExamScore()<<" ";
        cout<<fixed<<setprecision(2)<<studentScore.getFinalScore()<<endl;
    }
    cout<<"共"<<studentScoreList.size()<<"条记录."<<endl;
    return 0;
}

int StudentScoreService::showStudentScoreList(vector<StudentScore> studentScoreList) {
    for(StudentScore studentScore:studentScoreList){
        cout<<studentScore.getStudentId()<<" ";
        cout<<studentScore.getCourseId()<<" ";
        cout<<studentScore.getSchoolYear()<<" ";
        cout<<studentScore.getSemester()<<" ";
        cout<<fixed<<setprecision(2)<<studentScore.getRegularScore()<<" ";
        cout<<fixed<<setprecision(2)<<studentScore.getExamScore()<<" ";
        cout<<fixed<<setprecision(2)<<studentScore.getFinalScore()<<endl;
    }
    cout<<"共"<<studentScoreList.size()<<"条记录."<<endl;
    return 0;
}



StudentScoreService::StudentScoreService() {
}

StudentScoreService::~StudentScoreService() {

}